<template>
  <div class="app-container">
    <div class="tableBar" style="margin: 10px 55px ">
      <el-form :inline="true" :model="searchObj" class="demo-form-inline">
        <el-form-item label="分类编号" style="margin: 0 10px">
          <el-input
            v-model="searchObj.id"
            style="width: 210px"
            placeholder="分类编号"
            clearable
            @clear="getCategoryList"
            @keyup.enter.native="getCategoryList"
          ></el-input>
        </el-form-item>
        <el-form-item label="分类名字" style="margin: 0 10px">
          <el-input
            v-model="searchObj.categoryName"
            style="width: 120px"
            placeholder="分类名字"
            clearable
            @clear="getCategoryList"
            @keyup.enter.native="getCategoryList"
          ></el-input>
        </el-form-item>
        <el-form-item style="margin: 0 10px">
          <el-button type="primary" @click="getCategoryList">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="danger" @click="handleBatchDelete">批量删除</el-button>
        </el-form-item>

        <el-button plain style="float: right;margin-right: 20px;margin-top: 20px" @click="showAddDialog">新增分类
        </el-button>
      </el-form>

    </div>

    <el-table
      ref="multipleTable"
      @selection-change="handleSelectionChange"
      :data="tableData"
      style="width: 90%;margin:20px auto;"
      row-key="id"
      border
      :tree-props="{children: 'childCategory', hasChildren: 'hasChildren'}">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        prop="id"
        label="分类编号"
        sortable
        width="180">
      </el-table-column>
      <el-table-column
        prop="categoryName"
        label="分类名"
        width="180">
      </el-table-column>
      <el-table-column
        prop="descrip"
        label="描述">
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="140">
        <template slot-scope="scope">
          <el-button
            @click="handleUpdate(scope.row.id)"
            icon="el-icon-edit"
            circle
            style="margin:  1px 0 0  10px "
            size="mini"
            type="primary">
          </el-button>
          <el-button
            icon="el-icon-delete"
            circle
            style="margin: 1px 0 1px 10px "
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="dialog-container">
      <el-dialog title="修改分类信息" :visible.sync="dialogFormVisible">
        <el-form :model="category">
          <el-form-item label="父级分类" :label-width="formLabelWidth" v-if="category.parentId !==0">
            <el-select v-model="category.parentName" placeholder="请选择父级分类" @change="handleSelection">
              <el-option
                v-for="item in topCategory"
                :key="item.id"
                :label="item.categoryName"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="分类名称" :label-width="formLabelWidth">
            <el-input v-model="category.categoryName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="分类描述" :label-width="formLabelWidth">
            <el-input type="textarea" v-model="category.descrip" autocomplete="off"></el-input>
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="updateData">保 存</el-button>
        </div>
      </el-dialog>
    </div>

    <div class="paginationBar">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="this.current"
        :page-sizes="[4, 8, 12,16]"
        :page-size="this.pageSize"
        layout="total, prev, pager, next, sizes"
        :total="total"
        style="width: 30% ;margin: 5px auto">
      </el-pagination>
    </div>

    <el-dialog title="新增信息" :visible.sync="addDialogFormVisible" @close="clearData">
      <div class="radio-container" style="margin-left: 100px;margin-bottom: 20px">
        <el-radio v-model="category.isTop" :label=1>新增顶级分类</el-radio>
        <el-radio v-model="category.isTop" :label=0>新增次级分类</el-radio>
      </div>
      <el-form :model="category">
        <el-form-item label="父级分类" :label-width="formLabelWidth" v-if="category.isTop === 0">
          <el-select v-model="category.parentId" placeholder="请选择父级分类" @change="handleSelection">
            <el-option
              v-for="item in topCategory"
              :key="item.id"
              :label="item.categoryName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类名称" :label-width="formLabelWidth">
          <el-input v-model="category.categoryName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="分类描述" :label-width="formLabelWidth">
          <el-input type="textarea" v-model="category.descrip" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveData">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import categoryAPI from "@/api/category";


export default {

  data() {
    return {
      addDialogFormVisible: false,
      dialogFormVisible: false,
      isParent: false,
      category: {isTop: 0},
      searchObj: {},
      current: 1,
      pageSize: 4,
      filterText: '',
      tableData: [],
      deleteBatchIds: [],
      multipleSelection: [],
      total: 0,
      topCategory: [],
      formLabelWidth: '120px'
    }
  },
  created() {
    this.init()
  },

  methods: {
    clearData() {
      this.category = {isTop: 0}
    },
    saveData() {
      if (this.category.isTop === 1) {
        this.category.parentId = 0
      }
      categoryAPI.saveCategory(this.category).then((res) => {
        this.$message({
          type: 'success',
          message: '新增成功！'
        })
        this.addDialogFormVisible = false
        this.init()
      })
    },
    showAddDialog() {
      this.addDialogFormVisible = true
    },
    handleSelection(val) {
      this.category.parentId = val
    },
    handleUpdate(id) {
      this.dialogFormVisible = true
      categoryAPI.getById(id).then(res => {
        this.category = res.data
      })
    },
    // 根据id更新记录
    updateData() {
      categoryAPI.updateById(this.category.id, this.category).then(response => {
        this.$message({
          type: 'success',
          message: '修改成功！'
        })
        this.dialogFormVisible = false
        this.init()
      })
    },
    handleDelete(index, row) {
      this.$confirm('是否要删除该条信息', '删除', {
        distinguishCancelAndClose: true,
        cancelButtonText: '取消',
        confirmButtonText: '确定删除'
      })
        .then(() => {
          categoryAPI.deleteCategory(row.id).then(() => {
            this.$message({
              type: 'success',
              message: '删除成功！'
            })
          })
          this.current = 1
          this.getCategoryList()
        })
        .catch(action => {
          this.$message({
            type: 'info',
            message: '取消删除'
          })
        })
    },
    handleCurrentChange(val) {
      this.current = val
      this.getCategoryList()
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getCategoryList()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
      this.deleteBatchIds = []
      this.multipleSelection.forEach(row => {
        this.deleteBatchIds.push(row.id)
      })
      console.log(this.deleteBatchIds)
    },
    handleBatchDelete(index, row) {
      if (this.deleteBatchIds.length === 0) {
        this.$message({
          type: 'warning',
          message: '未选择删除对象'
        })
        return
      } else {
        this.$confirm('是否要删除该条信息', '删除', {
          distinguishCancelAndClose: true,
          cancelButtonText: '取消',
          confirmButtonText: '确定删除'
        })
          .then(() => {
            categoryAPI.deleteBatchCategory(this.deleteBatchIds).then(() => {
              this.$message({
                type: 'success',
                message: '删除成功！'
              })
              this.current = 1
              this.init()
            })
          })
          .catch(action => {
            this.$message({
              type: 'info',
              message: '取消删除'
            })
          })
      }
    },

    init() {
      categoryAPI.getTopCategory().then(res => {
        this.topCategory = res.data
      })
      this.getCategoryList()
    },
    getCategoryList() {
      categoryAPI.getCategoryPage(this.current, this.pageSize, this.searchObj).then(res => {
          this.tableData = res.data.records
          this.total = res.data.total
        }
      )
    },
  }
}
</script>

